# -*- coding：utf-8 -*-

import requests
import  MySQLdb
from scrapy.selector import Selector

conn = MySQLdb.connect(host="localhost", user="root", password="root123", db="article_spider", charset="utf8")
cursor = conn.cursor()



def crawl_ips():
    #爬取西刺的免费代理IP
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"
    }
    for i in range(4045):
        re = requests.get("https://www.xicidaili.com/nn/{0}".format(i), headers=headers)

        selector = Selector(text=re.text)
        all_trs = selector.css("#ip_list tr")

        ip_list = []
        for tr in all_trs[1:]:
            speed_str = tr.css(".bar::attr(title)").extract()[0]
            if speed_str:
                speed = float(speed_str.split("秒")[0])
            all_texts = tr.css("td::text").extract()

            ip = all_texts[0]
            port = all_texts[1]
            proxy_type = all_texts[5]

            ip_list.append((ip, port, proxy_type, speed))

        for ip in ip_list:
            cursor.execute(
                "insert into proxy_ip(ip, port, speed, proxy_type) values ('{0}','{1}',{2},'{3}')".format(
                    ip[0], ip[1], ip[3], ip[2]
                )
            )
            conn.commit()

class GetIp(object):
    def delete_ip(self,  ip):
        #从数据库中删除无效的Ip
        delete_sql = """
            delete from proxy_ip where ip='{0}'
        """.format(ip)
        cursor.execute(delete_sql)
        conn.commit()
        return True

    def judgu_ip(self, ip, port):
        #判断IP是否可用
        http_url = "http://www.baidu.com"
        proxy_url = "http://{0},{1}".format(ip,port)
        https_url = "https://{0},{1}".format(ip, port)
        try:
            proxy_dict = {
                "http":proxy_url,
                "https":https_url
            }
            requests.get(http_url, proxies=proxy_dict)
            return True
        except Exception as e:
            print("invalid ip and port")
            self.delete_ip(ip)
            return False
        else:
            code = response.status_code
            if code >= 200 and code< 300:
                print("effective ip")
                return True
            else:
                print("invalid ip and port")
                self.delete_ip(ip)
                return False

    def get_random_ip(self):
        #从数据库中随机获取一个可用的IP
        random_sql = """select ip, port from proxy_ip
                order by rand()
                limit 1 
                """
        cursor.execute(random_sql)
        for ip_info in cursor.fetchall():
            ip = ip_info[0]
            port = ip_info[1]

            judge_result = self.judgu_ip(ip, port)
            if judge_result:
                return "http://{0}:{1]".format(ip, port)
            else:
                return self.get_random_ip()


# print(crawl_ips())
if __name__ ==  "__main__":
    get_ip = GetIp()
    get_ip.get_random_ip()